package org.easy.web.api.db;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.easy.db.DataBaseInfo;
import org.easy.db.ExecuteSqlBody;
import org.easy.db.generator.GeneratorBody;
import org.easy.tool.res.Result;
import org.easy.web.service.db.DataBaseService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: wxl
 * @CreateTime: 2023/1/17 8:46
 */
@Api(value = "数据库操作API",tags = "数据库操作API")
@RestController
@RequestMapping("api/db/")
public class EasyDbController {

    private final DataBaseService dataBaseConnectService;

    public EasyDbController(DataBaseService dataBaseConnectService) {
        this.dataBaseConnectService = dataBaseConnectService;
    }

    @ApiOperation(value = "根据数据库信息创建连接",httpMethod = "POST")
    @ApiImplicitParam(name = "base",value = "数据库基本信息",required = true)
    @PostMapping("connect")
    public Result<Boolean> connect(@RequestBody DataBaseInfo base){
        return Result.success(dataBaseConnectService.connect(base));
    }

    @ApiOperation(value = "根据数据库名查找所有表名",httpMethod = "GET")
    @ApiImplicitParam(name = "dataBase",value = "数据库名",required = true)
    @GetMapping("queryTableName")
    public Result<Map<String, List<String>>> queryTableName (@RequestParam("dataBase") String dataBase) {
        return Result.success(dataBaseConnectService.getTableName(dataBase));
    }

    @ApiOperation(value = "根据数据库执行SQL语句",httpMethod = "POST")
    @ApiImplicitParam(name = "body",value = "执行SQL语句",required = true)
    @PostMapping("executeSql")
    public Result<?> executeSql(@RequestBody ExecuteSqlBody body){
        return dataBaseConnectService.executeSql(body.getDataBase(), body.getSql());
    }

    @ApiOperation(value = "根据根据表名生成代码",httpMethod = "POST")
    @ApiImplicitParam(name = "body",value = "生成代码参数",required = true)
    @PostMapping("generator")
    public Result<?> generator(@RequestBody GeneratorBody body){
         dataBaseConnectService.generator(body);
         return Result.success();
    }

}
